﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using MShop.Core.Service.Search;


namespace MShop.Core.Domain
{
    [MetadataType(typeof(Article_Validation))]
    public partial class Article : ISearchableContent
    {
        public Article()
        {
            this.Created = DateTime.Now;
            this.Modified = DateTime.Now;
            IsRedirect = false;
            this.Click = 100;
        }
        public bool IsRedirect { get; set; }

        public string SlashContent
        {
            get
            {
                return NoHTML(Content);
            }
        }

        public string SlashEncodeTitle(int length)
        {
            var length2 = this.Title.Length;
            if (length > length2)
                return Title;
            else
                return Title.Substring(0, length);
        }

        public string SlashEncodeContent(int length)
        {

            var length2 = SlashContent.Length;
            if (length > length2)
                return NoHTML(SlashContent);
            else
            {
                string mycontent = NoHTML(SlashContent.Substring(0, length));
                return string.Concat(mycontent, "...");
            }


        }

        private string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);//"
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);//&
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&mdash;", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            // Htmlstring = Htmlstring.Replace("&ldquo;", "");
            Htmlstring = (Htmlstring).Trim();
            string originalHtmlstring = Htmlstring;
            string lastCharacter = Htmlstring.Substring(Htmlstring.Length - 1, 1);

            Regex rx = new Regex("^[\u4e00-\u9fa5]$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            while (!rx.IsMatch(lastCharacter))
            {
                if (Htmlstring.Length <= 1)
                {
                    return originalHtmlstring;
                }
                Htmlstring = Htmlstring.Substring(0, Htmlstring.Length - 1);

                lastCharacter = Htmlstring.Substring(Htmlstring.Length - 1, 1);
                if (Htmlstring.Length < 1)
                {
                    return originalHtmlstring;
                }
            }
            return Htmlstring;
        }


        #region ISearchableContent Members

        public string ToSearchContent(ITextExtractor textExtractor)
        {
            return this.Content;
        }

        public IList<CustomSearchField> GetCustomSearchFields()
        {
            return new List<CustomSearchField>();
        }

        public SearchContent ToSearchContent()
        {
            SearchContent search = new SearchContent();
            search.Category = this.ArticleCategory.Name;
            search.DateCreated = this.Created;
            search.DateModified = this.Modified.HasValue ? this.Modified.Value : this.Created;
            search.Id = this.Id.ToString();
            search.Summary = NoHTML(Content);
            search.Title = this.Title;
            search.Author = string.IsNullOrEmpty(this.Author)?"webmaster":Author;
            search.Path = "/article/item/" + search.Id;
            search.Boost = 1f;
            return search;            
        }

        #endregion
    }

    internal partial class Article_Validation
    {
        [DisplayName("文章标题")]
        [Required(ErrorMessage="文章标题不能为空")]
        public string Title { get; set; }
      
        [DataType(DataType.MultilineText)]
        [DisplayName("文章摘要")]
        public string Summary { get; set; }

        [DisplayName("文章分类")]
        public int CId { get; set; }

        [DisplayName("排序")]
        public int Sort { get; set; }

        [DisplayName("是否外链")]
        public bool IsRedirect { get; set; }


        [DisplayName("标题颜色")]
        public string TitleColor { get; set; }


        [Foolproof.RequiredIfFalse("IsRedirect", ErrorMessage = "文章内容不能为空")]
        [DataType(DataType.MultilineText)]
        [DisplayName("文章内容")]
     
        public string Content { get; set; }

        [Required(ErrorMessage = "创建日期不能为空")]
        [DisplayName("创建日期")]
        public DateTime  Created { get; set; }

        [DisplayName("文章来源")]
        public string Source { get; set; }

        [DisplayName("文章作者")]
        public string Author { get; set; }

      
        [Foolproof.RequiredIfTrue("IsRedirect", ErrorMessage = "外链地址不能为空，请使用http://开头")]
        [DisplayName("外链地址")]
        public string RedirectUrl { get; set; }
    }
}